package com.cssw.fyzb.tps.service.im.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cssw.fyzb.tps.util.HttpUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author Jian Qin.
 */
@AllArgsConstructor
@Component
public class ImHttpComm {

    private String doman = "https://console.tim.qq.com/";

    private String apiVersion = "v4";

    private long sdkappId;

    private String identifier;

    private String key;


    public ImResult post(String fun, Map params){
        return post(fun, JSON.toJSONString(params));
    }

    public ImResult post(String fun, String str){
        String json = null;
        Map<String, Object> ret = null;
        try {
            json = HttpUtil.post(generateUrl(fun), str);

            ret = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
        } catch (Exception e) {
            e.printStackTrace();
            return except(e);
        }
        return build(ret);
    }

    public String generateUrl(String function){
        TLSSigAPIv2 tls = new TLSSigAPIv2(this.sdkappId, this.key);
        StringBuffer buffer = new StringBuffer();
        buffer.append(this.doman)
                .append(apiVersion)
                .append(function)
                .append("?")
                .append("sdkappid=")
                .append(this.sdkappId)
                .append("&identifier=")
                .append(this.identifier)
                .append("&usersig=")
                .append(tls.genUserSig(this.identifier, 24*60*60))
                .append("&random=")
                .append(generateRandom())
                .append("&contenttype=json");
        return buffer.toString();
    }

    private long generateRandom(){
        Random r = new Random(new Date().getTime());
        return r.nextInt();
    }

    private ImResult build(Map<String, Object> map){
        ImResult imResult = new ImResult();
        imResult.setActionStatus(String.valueOf(map.get("ActionStatus")));
        imResult.setErrorCode((int)map.get("ErrorCode"));
        imResult.setErrorInfo(String.valueOf(map.get("ErrorInfo")));
        imResult.setData(map);
        return imResult;
    }

    private ImResult except(Exception e){
        ImResult imResult = new ImResult();
        imResult.setActionStatus("Exception");
        imResult.setErrorCode(500);
        imResult.setErrorInfo(e.getMessage());
        return imResult;
    }



    @Data
    public class ImResult{
        private String actionStatus;

        private String errorInfo;

        private int errorCode;

        private Map<String, Object> data;

        public String getString(String key){
            return String.valueOf(data.get(key));
        }

        public Integer getInteger(String key){
            return (Integer)data.get(key);
        }

        public Long getLong(String key){
            return (Long)data.get(key);
        }

        public List getList(String key){
            return (List) data.get(key);
        }

        public Object getObject(String attrNames) {
            return data.get(key);
        }
    }
}
